package com.cn.filter;

import com.sun.net.httpserver.HttpExchange;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse res = (HttpServletResponse) response;
        String uri = req .getRequestURI();
        if("login".equals(uri)||"regist".equals(uri)){
            filterChain.doFilter(request,response);
        }
        String token =req.getParameter("token");
        String time =req.getParameter("time");
        String id =req.getParameter("id");
        Long cutTime=System.currentTimeMillis();
        if("".equals(token)||token==null||Long.valueOf(time) > cutTime){
            res.setHeader("sessionstatus","timeout");
            res.setStatus(500);
            filterChain.doFilter(request,response);
            System.out.println("111111111111111");
            return;
        }else{
            filterChain.doFilter(request,response);
            System.out.println("111112222222221111111111");
        }
    }

    @Override
    public void destroy() {

    }
}
